package zhoujunhao.rbac.view;

import zhoujunhao.rbac.domian.dto.UserDto;
import zhoujunhao.rbac.domian.vo.DeptVo;
import zhoujunhao.rbac.domian.vo.Result;
import zhoujunhao.rbac.domian.vo.RoleVo;
import zhoujunhao.rbac.domian.vo.UserVo;
import zhoujunhao.rbac.service.IUserService;
import zhoujunhao.rbac.service.array.UserService;

public class Userview extends Baseview<UserVo, IUserService> {
    private static final String MENU = """
            ------------
            用户管理
            0.返回上级
            1.部门列表
            2.创建用户
            3.更新用户
            4.删除用户
            ------------
            请输入选项：
            """;
    private static Userview singleton;

    private final Deptview deptview;
    private final Roleview roleview;


    private Userview() {
        deptview = Deptview.getInstance();
        roleview = Roleview.getInstance();
        service = UserService.getInstance();
    }

    public static Userview getInstance() {
        if (singleton == null) {
            singleton = new Userview();
        }
        return singleton;
    }

    /**
     * 用户管理
     */
    public void show() {
        String menuChoice;
        do {
            System.out.println(MENU);
            menuChoice = Mainview.input.next();
            switch (menuChoice) {
                case "0":
                    System.out.println("已返回上一级菜单");
                    break;
                case "1":
                    list();
                    break;
                case "2":
                    create();
                    break;
                case "3":
                    update();
                    break;
                case "4":
                    toggle();
                    break;
                default:
                    System.out.println("输入错误，请重新输入");
            }
        } while (!"0".equals(menuChoice));

    }

    /**
     * 显示用户管理菜单
     */


    public void list() {
        Result result = service.list();
        cache = (UserVo[]) result.data;
        System.out.println("ID\t用户名\t姓名\t手机\t邮箱\t部门名称\t状态");
        for (UserVo user : cache) {
            System.out.printf("%d\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n", user.id, user.username, user.name,
                    user.moblie, user.email, user.deptName, user.roleName, user.enabled ? "启用" : "停用");
        }
    }

    public void create() {
        System.out.print("请输入用户名称: ");
        String name = Mainview.input.nextLine();
        System.out.print("请输入用户手机号码: ");
        String moblie = Mainview.input.nextLine();
        System.out.print("请输入用户邮箱: ");
        String email = Mainview.input.nextLine();

        System.out.print("请选择部门ID: ");
        int deptId = 0;
        do {
            deptview.list();
            if (deptview.cache == null || deptview.cache.length == 0) {
                printError("系统还没有创建任何信息，请先创建部门信息");
                return;
            }
            DeptVo dept = deptview.getCacheById();
            if (dept != null) {
                deptId = dept.id;
            }
        } while (deptId == 0);

        System.out.println("请选择角色ID: ");
        int roleId = 0;
        do {
            roleview.list();
            if (roleview.cache == null || roleview.cache.length == 0) {
                printError("系统还没有创建任何信息，请先创建角色信息");
                return;
            }
            RoleVo role = roleview.getCacheById();
            if (role != null) {
                roleId = role.id;
            }
        } while (roleId == 0);
        UserDto dto = new UserDto(name, name, moblie, email, deptId, roleId);
        Result result = service.create(dto);
        if (!result.success) {
            printError(result.message);
            return;
        }
        System.out.println(result.message);
        list();


    }

    public void update() {
        UserVo user = getCacheById();
        if (user == null) {
            return;
        }
        System.out.print("请输入用户名，直接按回车表示不修改：");
        String username = Mainview.input.nextLine();
        username = "".equals(username) ? user.username : username;
        System.out.print("请输入用户姓名，直接按回车表示不修改：");
        String name = Mainview.input.nextLine();
        name = "".equals(name) ? user.name : name;
        System.out.print("请输入用户手机号码，直接按回车表示不修改：");
        String moblie = Mainview.input.nextLine();
        moblie = "".equals(moblie) ? user.moblie : moblie;
        System.out.print("请输入用户邮箱，直接按回车表示不修改：");
        String email = Mainview.input.nextLine();
        email = "".equals(email) ? user.email : email;

        System.out.print("请选择部门ID: ");
        int deptId = 0;
        do {
            deptview.list();
            if (deptview.cache == null || deptview.cache.length == 0) {
                printError("系统还没有创建任何信息，请先创建部门信息");
                return;
            }
            DeptVo dept = deptview.getCacheById();
            if (dept != null) {
                deptId = dept.id;
            }
        } while (deptId == 0);

        System.out.println("请选择角色ID: ");
        int roleId = 0;
        do {
            roleview.list();
            if (roleview.cache == null || roleview.cache.length == 0) {
                printError("系统还没有创建任何信息，请先创建角色信息");
                return;
            }
            RoleVo role = roleview.getCacheById();
            if (role != null) {
                roleId = role.id;
            }
        } while (roleId == 0);


        UserDto dto = new UserDto(user.username, name, moblie, email, deptId, roleId);
        Result result = service.update(dto, user.id);
        if (!result.success) {
            printError(result.message);
            return;
        }
        System.out.println(result.message);
        list();
    }


    public void toggle() {
        UserVo user = getCacheById();
        if (user == null) {
            return;
        }

        if (user.enabled) {
            System.out.print("输入‘Y’确认停用，输入其他取消停用:");
        } else {
            System.out.print("输入‘Y’确认启用，输入其他取消启用:");
        }
        String confirm = Mainview.input.nextLine();
        if (!"Y".equalsIgnoreCase(confirm)) {
            System.out.println("您已经取消停用");
            return;
        }
        Result result = service.toggle(user.id);
        if (!result.success) {
            printError(result.message);
            return;
        }
        System.out.println(result.message);
        list();
    }


}
